#proevolytx@gmail.com
#2012/9/13

text = "1+2*3^(4-5)"

class brackets:
    def __init__(s, text):
        s.text = text
        s.index = 0
        if len(s.text):
            s.la = s.text[s.index]
        else:
            s.la = None
        s.op = [["+", "-"], ["*", "/"], ["^"]]
        s.out = ""
        
    def nextch(s):
        if s.index < len(s.text):
            s.index += 1
        if s.index < len(s.text):
            s.la = s.text[s.index]
        else:
            s.la = None

    def expr(s, piority):
        if piority >= len(s.op):
            s.factor()
            return
        s.expr(piority + 1)
        while True:
            if s.la in s.op[piority]:
                ch = s.la
                s.nextch()
                s.expr(piority + 1)
                s.out += ch
            else:
                break

    def factor(s):
        if s.la == "(":
            s.match("(")
            s.expr(0)
            s.match(")")
        else:
            s.out += s.la
            s.nextch()

    def match(s, ch):
        if s.la != ch:
            raise Exception(ch + " expected, " + s.la + " instead")
        else:
            s.nextch()

test = brackets(text)
test.expr(0)
print test.out
